﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.yeepay.bank;


namespace NJL.Web.Payment.yeepay
{
    public partial class Callback : BasePageVisitor
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 校验返回数据包
                BuyCallbackResult result = Buy.VerifyCallback(FormatQueryString.GetQueryString("p1_MerId"), FormatQueryString.GetQueryString("r0_Cmd"), FormatQueryString.GetQueryString("r1_Code"), FormatQueryString.GetQueryString("r2_TrxId"),
                    FormatQueryString.GetQueryString("r3_Amt"), FormatQueryString.GetQueryString("r4_Cur"), FormatQueryString.GetQueryString("r5_Pid"), FormatQueryString.GetQueryString("r6_Order"), FormatQueryString.GetQueryString("r7_Uid"),
                    FormatQueryString.GetQueryString("r8_MP"), FormatQueryString.GetQueryString("r9_BType"), FormatQueryString.GetQueryString("rp_PayDate"), FormatQueryString.GetQueryString("hmac"));

                if (string.IsNullOrEmpty(result.ErrMsg))
                {
                    //在接收到支付结果通知后，判断是否进行过业务逻辑处理，不要重复进行业务逻辑处理
                    if (result.R1_Code == "1")
                    {
                        if (result.R9_BType == "1")
                        {
                            //  callback方式:浏览器重定向
                            //Response.Write("支付成功!<br>商品ID:" + result.R5_Pid + "<br>商户订单号:" + result.R6_Order + "<br>支付金额:" + result.R3_Amt + "<br>易宝支付交易流水号:" + result.R2_TrxId + "<BR>");
                            if (result.R6_Order.Contains("T"))
                            {
                                NJL.BLL.NJL_TEMP_ORDER blltempOrder = new NJL.BLL.NJL_TEMP_ORDER();
                                NJL.Model.NJL_TEMP_ORDER modeltempOrder = blltempOrder.GetModelbyOrderId(result.R6_Order);
                                modeltempOrder.ORDER_STATE = 2;
                                modeltempOrder.PAY_DATE_TIME = DateTime.Now;
                                blltempOrder.Update(modeltempOrder);
                                Response.Redirect("/Shop/TempPaymentReturn.aspx?id=1&orderId=" + modeltempOrder.ID);
                            }
                            else
                            {
                                NJL.BLL.NJL_BOOK_ORDER bll = new NJL.BLL.NJL_BOOK_ORDER();
                                NJL.Model.NJL_BOOK_ORDER model = bll.GetModelByOrderNO(result.R6_Order);
                                model.ORDER_STATE = 2;          //付款成功，完成订单
                                model.PAY_DATE_TIME = DateTime.Now;
                                model.PAY_MAP_TYPE_CODE = "4";//更新支付方式
                                bll.Update(model);
                                NJL.BLL.NJL_BOOK bllBook = new NJL.BLL.NJL_BOOK();
                                NJL.Model.NJL_BOOK modelBook = bllBook.GetModel(model.NJL_BOOK_ID);
                                NJL.BLL.NJL_REG_PAY bllRegPay = new NJL.BLL.NJL_REG_PAY();
                                NJL.Model.NJL_REG_PAY modelRegPay;
                                modelRegPay = new NJL.Model.NJL_REG_PAY();
                                modelRegPay.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                                modelRegPay.NJL_SYS_BASE_ID = WEBSITE_CODE;
                                modelRegPay.BOOK_ORDER_ID = model.ID;
                                modelRegPay.ORDER_NO = result.R6_Order;
                                modelRegPay.ORDER_TOTAL = Convert.ToDecimal(result.R3_Amt);
                                modelRegPay.ORDER_TYPE_CODE = modelBook.TYPE_CODE;
                                modelRegPay.PAY_MAP_TYPE_CODE = "4";        //4为易宝
                                modelRegPay.CREATE_DATE_TIME = DateTime.Now;
                                modelRegPay.CREATE_USER_ID = UserId;
                                modelRegPay.CREATE_USER_NAME = UserName;
                                if (bllRegPay.Add(modelRegPay))
                                {

                                }
                                if (modelBook.TYPE_CODE == "1")
                                {
                                    Response.Redirect("/AttractionsOrderOK.aspx?id=" + model.ID, true);
                                }
                                else if (modelBook.TYPE_CODE == "2")
                                {
                                    if (model.IS_REGISTER_USER == 1)
                                    {
                                        Response.Redirect("/CompanyOrderOK.aspx?id=" + model.ID, true);
                                    }
                                    else
                                    {
                                        Response.Redirect("/CompanyOrderOK2.aspx?id=" + model.ID, true);
                                    }
                                }
                            }
                        }
                        else if (result.R9_BType == "2")
                        {
                            // * 如果是服务器返回则需要回应一个特定字符串'SUCCESS',且在'SUCCESS'之前不可以有任何其他字符输出,保证首先输出的是'SUCCESS'字符串
                            Response.Write("SUCCESS");
                        }
                    }
                    else
                    {
                        Response.Write("支付失败!");
                    }
                }
                else
                {
                    Response.Write("交易签名无效!");
                }
            }
        }
    }
}